package com.chainyoung.component_tmperson.ui.activity.debit;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.adapter.WalletDebitAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletDebitContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.WalletDebitPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;

import static com.chainyoung.router.RouterConstants.PATH_DEBIT;


//借贷产品界面
@Route(path = PATH_DEBIT)
public class DebitActivity extends BaseActivity<WalletDebitPresenter> implements WalletDebitContract.View {


    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private WalletDebitAdapter mAdapter;
    private List<WalletDebit> mData = new ArrayList<>();


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle(R.string.tmperson_debit);
        initRecyclerView();
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray_10));
        rvList.addItemDecoration(itemDecoration);
        mAdapter = new WalletDebitAdapter(mData);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //loadData();
                getData();
            }
        });
    }


    @Override
    protected boolean useLoadSir() {
        return true;
    }

    @Override
    protected Object registerTarget() {
        return refreshLayout;
    }

    @Override
    protected void onPageRetry(View v) {
        super.onPageRetry(v);
        getData();
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_debit;
    }

    private void getData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        mPresenter.getWalletDebitList(map);
    }


    @Override
    public void onGetAllDebitList(List<WalletDebit> datas) {
        refreshLayout.finishRefresh();
        mData.clear();
        if (datas != null) {
            mData.addAll(datas);
        }
        mAdapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            showPageEmpty();
        } else {
            showPageContent();
        }
    }

    @Override
    public void onGetAllDebitListFailed() {
        refreshLayout.finishRefresh();
        showPageError();
    }


}
